import { CustomErrorCode } from '@/api/enums'

import type { ErrorResponseData, SuccessResponseData } from '@/api/types'

type UserPayload = Record<'username' | 'password', string>
type VerifyCodePayload = Record<'uuid' | 'verifyCode', string>

export type LoginPayload = UserPayload | (UserPayload & VerifyCodePayload)

export type LoginResponseData =
  | SuccessResponseData<Record<'access_token', string>, '登录成功'>
  | ErrorResponseData<VerifyCodePayload, '用户名/密码错误'>
  | ErrorResponseData<VerifyCodePayload, '验证码已过期', CustomErrorCode.VERIFY_CODE_ERROR>
  | ErrorResponseData<VerifyCodePayload, '验证码错误', CustomErrorCode.VERIFY_CODE_ERROR>
  | ErrorResponseData<{}, '验证码错误过多', CustomErrorCode.VERIFY_CODE_FREQUENT>

export type GetVerifyCodeResponseData =
  | SuccessResponseData<VerifyCodePayload, '获取验证码成功'>
  | ErrorResponseData<{}, '验证码请求过于频繁', CustomErrorCode.VERIFY_CODE_FREQUENT>

export type SendPhoneVerifyCodeResponseData =
  | SuccessResponseData<{}, '已发送验证码'>
  | ErrorResponseData<{}, '验证码请求过于频繁', CustomErrorCode.VERIFY_CODE_FREQUENT>

export type RegisterPayload = UserPayload & Record<'phone' | 'verifyCode', string>

export type RegisterResponseData =
  | SuccessResponseData<Record<'access_token', string>, '注册成功'>
  | ErrorResponseData<{}, '用户名已存在', CustomErrorCode.BAD_REQUEST>
  | ErrorResponseData<{}, '验证码已过期', CustomErrorCode.VERIFY_CODE_EXPIRED>
  | ErrorResponseData<{}, '验证码错误', CustomErrorCode.VERIFY_CODE_ERROR>
  | ErrorResponseData<{}, '验证码错误过多', CustomErrorCode.VERIFY_CODE_FREQUENT>
